<template>
    <el-dialog title="审核" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
         <el-form-item label="审核结果" prop="check_status">
          <el-radio-group v-model="form.check_status">
            <el-radio
              v-for="dict in dict.type.flow_check_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="原因" prop="check_reason" v-if="form.check_status == '3'">
          <el-input v-model="form.check_reason" type="textarea" rows="3" placeholder="请输入审核不通过原因" />
        </el-form-item>
        <el-form-item label="后续操作" prop="nextAction" v-if="form.check_status == '2'">
          <el-radio-group v-model="form.nextAction">
            <el-radio
              v-for="dict in nextOptions"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="下一步" prop="nextUserId" v-if="form.check_status == '2' && form.nextAction == '2'">
         <el-select v-model="form.nextUserId" placeholder="请选择审核人" clearable multiple>
          <el-option
            v-for="u in userOptions"
            :key="u.value"
            :label="u.label"
            :value="u.value"
          />
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { restGet, apiPost, apiPatch, apiDs, apiDownload } from "@/api/openapi";

export default {
  name: "CheckDialog",
  dicts: ['flow_check_status'],
  props:{
    //数据模型
      applyData: Object,
  },
  data() {
    return {
      open: false,
      nextOptions: [
        {
          value:"1",
          label:"结束审批"
        },{
          value:"2",
          label:"继续审批"
        }
      ],
      userOptions: [],
      form:{},
      // 表单校验
      rules: {
        nextUserId: [
          { required: true, message: "审核人不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    apiDs('sys_user_select').then(response=>{
        this.userOptions = response.data;
    });
  },
  watch: {
    applyData: function(newVal, oldVal){
      if(newVal == null){
        this.open = false;
      }else{
        this.reset();
        this.open = true;
      }
    }
  },
  methods: {
    reset() {
      this.form = {
        id:this.applyData.flow_id,
        nextUserId: [],
        flow_model: this.applyData.flow_model,
        data_id: this.applyData.data_id,
        check_status: null,
        check_reason: null,
        nextAction: '2'
      }
    },
    /** 取消按钮操作 */
    cancel() {
      this.$emit('cancel');
    },
    // 提交上传文件
    submit() { 
      this.$refs["form"].validate(valid => {
        if (valid) {          
            let data = {"asset_flow":[], "@tags":['asset_flow']};            
            this.form['check_time'] = "@now()";
            data['asset_flow'].push(this.form);
            //审批通过后，可选择结束或者继续审批
            if(this.form['check_status'] == '2'){
              //审批结束
              if(this.form.nextAction == '1'){
                data[this.applyData['flow_model']] = {
                  id: this.applyData.data_id,
                  check_status: '2'
                }
                data["@tags"].push(this.applyData['flow_model']);
              }else{
                //继续审批
                if(!this.form.nextUserId || this.form.nextUserId.length == 0){
                  this.$modal.msgError("请选择下一步审批人");
                  return;    
                }
                for(var i=0;i<this.form.nextUserId.length;i++){                  
                  let userName = null;
                  for(var j=0;j<this.userOptions.length;j++){
                    if(this.userOptions[j].value == this.form.nextUserId[i]){
                      userName = this.userOptions[j].label;
                    }
                  }
                  data['asset_flow'].push({
                    'flow_model': this.applyData['flow_model'],
                    'data_id': this.applyData['data_id'],
                    'check_user_id': this.form.nextUserId[i],
                    'check_user_name': userName,
                    'check_status':'1'
                  })
                }
              }
            }else{
              //审批不通过
               data[this.applyData['flow_model']] = {
                  id: this.applyData.data_id,
                  check_status: '3'
                }
                data["@tags"].push(this.applyData['flow_model']);
            }
            apiPatch(data).then(response => {
              this.$modal.msgSuccess("审批成功");
              this.open = false;
              this.$emit('success');
            });            
        }
      });
    }
  }
};
</script>
